package com.anosine.gpsl.android.base;

import com.anosine.gpsl.android.util.ToastHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;

/**
 * @Title: BaseAsyncTask
 * @Package com.anosine.gpsl.android.base
 * @Description: 
 * @author: sunny
 * @date: 2015-11-24
 * @version V1.0
 */
public abstract class BaseAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

    private Context mContext;
    private String mErrorMsg;
    private Exception mException;
    private Loading mLoading;
    private String mTitle;
   
    /**
	 * @Title: BaseAsyncTask(ILoading loading)
	 * @Description: 构造函数，初始化
	 * @param: loading
	 * @throws
	 */
    public BaseAsyncTask(ILoading loading) {
        this(loading, false);
    }
    /**
	 * @Title: BaseAsyncTask(ILoading loading, boolean showLoading)
	 * @Description: 构造函数，初始化
	 * @param: loading
	 * @param: showLoading
	 * @throws
	 */
    public BaseAsyncTask(ILoading loading, boolean showLoading) {
        this(loading, showLoading, null);
    }
    /**
	 * @Title: BaseAsyncTask(ILoading loading, boolean showLoading, String title)
	 * @Description: 构造函数，初始化
	 * @param: loading
	 * @param: showLoading
	 * @param: title
	 * @throws
	 */
    public BaseAsyncTask(ILoading loading, boolean showLoading, String title) {
        super();
        if (loading != null) {
            mContext = loading.getLoadingContext();
        }
        if (showLoading) {
            mLoading = loading.getLoading();
            mTitle=title;
        }
    }
    /**
   	 * @Title: getContext() 
   	 * @Description: 获取context
   	 * @param: 无
   	 * @return: Context
   	 * @throws
   	 */
    public Context getContext() {
        return mContext;
    }
    /**
   	 * @Title: getException() 
   	 * @Description: 获取异常
   	 * @param: 无
   	 * @return: Exception
   	 * @throws
   	 */
    protected Exception getException() {
        return mException;
    }
    /**
   	 * @Title: onPreExecute() 
   	 * @Description: 显示mLoading
   	 * @param: 无
   	 * @return: 无
   	 * @throws
   	 */
    @Override
    protected final void onPreExecute() {
        if (null != mLoading) {
            try {
                if (!(mTitle == null || mTitle.trim().length() <= 0)){
                    mLoading.show(mTitle);
                }else{
                    mLoading.show();
                }
            } catch (Exception e) {
            }
        }
    }
    /**
   	 * @Title: doInBackground(Params... params) 
   	 * @Description: 
   	 * @param: params
   	 * @return: Result
   	 * @throws
   	 */
    @Override
    protected final Result doInBackground(Params... params) {
        Result result = null;
        try {
            result = onLoad(params);
        } catch (Exception e) {
            e.printStackTrace();
            mException = e;
            mErrorMsg = e.getMessage();
        }
        return result;
    }
    /**
   	 * @Title: onPostExecute(Result result)
   	 * @Description: 执行后
   	 * @param: Result
   	 * @return: 无
   	 * @throws
   	 */
    @Override
    protected final void onPostExecute(Result result) {
        if (!isCancelled()) {
            if (null != mErrorMsg) {
                onHandleError(mErrorMsg);
            } else {
                onUI(result);
            }
        }
        if (null != mLoading) {
            try {
                mLoading.dismiss();
            } catch (Exception e) {
            }
        }
    }
    /**
   	 * @Title: onLoad(Params... params)
   	 * @Description: 执行后
   	 * @param: params
   	 * @return: Result
   	 * @throws
   	 */
    protected abstract Result onLoad(Params... params)
            throws Exception;
    /**
   	 * @Title: onUI(Result result)
   	 * @Description: 
   	 * @param: Result
   	 * @return: 无
   	 * @throws
   	 */
    protected abstract void onUI(Result result);
    /**
   	 * @Title: onHandleError(String errorMsg)
   	 * @Description: 显示错误信息
   	 * @param: errorMsg
   	 * @return: 无
   	 * @throws
   	 */
    protected void onHandleError(String errorMsg) {
        ToastHelper.toast(errorMsg);
    }
    /**
   	 * @Title: executeParallelly(Params... params)
   	 * @Description: 并行执行
   	 * @param: params
   	 * @return: 无
   	 * @throws
   	 */
    @SuppressLint("NewApi")
	public final void executeParallelly(Params... params) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            execute(params);
        } else {
            executeOnExecutor(THREAD_POOL_EXECUTOR, params);
        }
    }
}
